/**********************************************************************
  The MIT License (MIT)
  Copyright (c) 2012 sunriseyuen

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/

#define _DEF_CCY_CODE
#define _ZZDEBUG
#define _PROJ_DC

#region Using directives
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

using Muki.Framework.Util;
using Muki.Framework.Business;
using Muki.Framework;
using Muki.UserDefine;
using Muki.Entity;
using Dot.StreamObject;

#endregion

namespace Muki.UserPanel
{
	public class UID02805AM:BLogic
	{
		public UID02805AM()
		{
			objDefault.UID_CODE    ="UID02805A";
		}

		public void Initialize()
		{
			objDefault.ZZLOGON_SSD = this.ZZLOGON_SSD;
			objDefault.Trans       = this.Trans;
			objDefault.ZZLOGON_ID  = this.ZZLOGON_ID;

			_Convert.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_Convert.Trans       = this.Trans;

			_DataPermission.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_DataPermission.Trans       = this.Trans;
			_DataPermission.TABLE_NAME  = "ECNHDTM";
			_DataPermission.ZZLOGON_ID  = this.ZZLOGON_ID;
			_DataPermission.DataAccessRight();

			_DocsPermission.ZZLOGON_SSD  = this.ZZLOGON_SSD;
			_DocsPermission.Trans        = this.Trans;

			_Setting.ZZLOGON_ID = this.ZZLOGON_ID;
			_Setting.Trans      = this.Trans;

		}

		public UID02805AE Verify(UID02805AE _Entity)
		{

						_Entity.ECN_NO=_Entity.ECN_NO.Trim();

					if (Util.IsNullOrEmpty(_Entity.ODTP_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("ODTP_CODE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.ECN_DATE))
					{
						_Entity.Exceptions.Add(new LogisticException("ECN_DATE","DataMiss"));
					}

				// Relation ZUODTPTM
			if (!Util.IsNullOrEmpty(_Entity.ODTP_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ZUODTPTMEntity));//单据类型
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ODTP_CLASS_CODE","ECN");
			objCondition.Equals("ODTP_CODE",_Entity.ODTP_CODE);
				_DataPermission.TABLE_NAME="ZUODTPTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("ODTP_CODE","DataNotFd",_Entity.ODTP_CODE));
			}else{

					ZUODTPTMEntity objVZUODTPTMEntity=(ZUODTPTMEntity)objEntityContainer[0];
					if (objVZUODTPTMEntity.Verified)
					{
						 _Entity.ODTP_CODE=objVZUODTPTMEntity.ODTP_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("ODTP_CODE","DataNotFd",_Entity.ODTP_CODE));
					}
			}
			}
//ZUODTPTM

				// Relation ZNCOMPTM
			if (!Util.IsNullOrEmpty(_Entity.COMP_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ZNCOMPTMEntity));//公司资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("COMP_CODE",_Entity.COMP_CODE);
				_DataPermission.TABLE_NAME="ZNCOMPTM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("COMP_CODE","DataNotFd",_Entity.COMP_CODE));
			}else{

					ZNCOMPTMEntity objVZNCOMPTMEntity=(ZNCOMPTMEntity)objEntityContainer[0];
					if (objVZNCOMPTMEntity.Verified)
					{
						 _Entity.COMP_CODE=objVZNCOMPTMEntity.COMP_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("COMP_CODE","DataNotFd",_Entity.COMP_CODE));
					}
			}
			}
//ZNCOMPTM

				return _Entity;
		}

		public List<UID02805AE> Verify(List<UID02805AE> _Entitys)
		{
			List<UID02805AE> _New_Entitys=new List<UID02805AE>();

			foreach (UID02805AE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Verify(_Entity));
			}
			return _New_Entitys;
		}

		public UID02805AE Default(UID02805AE _Entity)
		{

						_Entity.ECN_NO=objDefault.getDefault("ECN_NO",_Entity.ECN_NO);
						_Entity.ODTP_XREV=objDefault.getDefault("ODTP_XREV",_Entity.ODTP_XREV);
						_Entity.ODTP_STATUS=objDefault.getDefault("ODTP_STATUS",_Entity.ODTP_STATUS);
						_Entity.ODTP_CODE=objDefault.getDefault("ODTP_CODE",_Entity.ODTP_CODE);
						_Entity.COMP_CODE=objDefault.getDefault("COMP_CODE",_Entity.COMP_CODE);
						_Entity.ECN_DATE=objDefault.getDefault("ECN_DATE",_Entity.ECN_DATE);
						_Entity.ECN_ODTP_RMKS=objDefault.getDefault("ECN_ODTP_RMKS",_Entity.ECN_ODTP_RMKS);

				return _Entity;
		}

		public List<UID02805AE> Default(List<UID02805AE> _Entitys)
		{
			List<UID02805AE> _New_Entitys=new List<UID02805AE>();
			foreach (UID02805AE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Default(_Entity));
			}
			return _New_Entitys;
		}

		public UID02805AE Search(VContexts _VContexts,UID02805AE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02805A", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02805A","NotAccessRight", this.ZZLOGON_ID));
				return _Entity;
			}

			ZZSQLStatement  _SQLStatement = new ZZSQLStatement();

			_SQLStatement.Trans         = this.Trans;
			_SQLStatement.UID_CODE      = "UID02805A";
			_SQLStatement.ZZLOGON_SSD   = this.ZZLOGON_SSD;
			_SQLStatement.ZZLOGON_LANG  = this.ZZLOGON_LANG;
			_SQLStatement.DbName        = this.DbName;
			_SQLStatement.Process("QID02805A");

			ZZQLite oZZQLite = _SQLStatement.oZZQLite;

			if (_VContexts.getValue("T1")!="" && _VContexts.getValue("V1")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T1"), _VContexts.getValue("V1"));
			}
			if (_VContexts.getValue("T2")!="" && _VContexts.getValue("V2")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T2"), _VContexts.getValue("V2"));
			}
			if (_VContexts.getValue("T3")!="" && _VContexts.getValue("V3")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T3"), _VContexts.getValue("V3"));
			}

			if (!Util.IsNullOrEmpty(_VContexts.getValue("lookup")))
			{
				oZZQLite.AddXWhereClause("A", "", _VContexts.getValue("lookup"));
			}

			oZZQLite.Condition=_VContexts.getValue("Condition");
			oZZQLite.Make();

			int _AbsolutePage = _VContexts.getInteger("AbsolutePage");
			int _PageSize     = _VContexts.getInteger("ZZPageSize");
			int _Restrict     = _VContexts.getInteger("Restrict");
			int _TotalPages   = 0;

			_Setting.Trans      = this.Trans;

			if (_Restrict == 0)
			{
				_Restrict=30;
			}

			if (_PageSize <= 0)
			{
				_PageSize=_Setting.getInteger("ZZPageSize","20");
			}

				QueryRows RsECNHDTM   = new QueryRows(this.Trans);
			RsECNHDTM.CommandText = oZZQLite.SQLStatement;
			RsECNHDTM.Top         = _Restrict;
			RsECNHDTM.Skip        = (_AbsolutePage-1)*_PageSize;
			RsECNHDTM.Limit       = _PageSize;
			RsECNHDTM.CountAll    = true;
			RsECNHDTM.Open();

			if (_PageSize <= 0)
			{
				_TotalPages   = RsECNHDTM.RecordCount;
				_PageSize = RsECNHDTM.RecordCount;
			}
			else
			{
				_TotalPages= RsECNHDTM.RecordCount / _PageSize;
			}
			if (RsECNHDTM.RecordCount > (RsECNHDTM.RecordCount / _PageSize) * _PageSize)
			{
				_TotalPages = _TotalPages+ 1;
			}

			if (_AbsolutePage <= 0)
			{
				_AbsolutePage = 1;
			}

			if (_AbsolutePage > _TotalPages)
			{
				_AbsolutePage = _TotalPages;
			}

			Wxf _Wxf  = new Wxf();

			_Wxf.XDeclareStart();
			foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
			{
				if (_AttributeMapping.STATUS_CODE != "HIDDEN")
				{
					_Wxf.XDeclare(_AttributeMapping);
				}
			}
			_Wxf.XDeclareFinal();

			_Wxf.Variable("ZZPageSize",_PageSize);
			_Wxf.Variable("ZZPages",_TotalPages);
			_Wxf.Variable("USQL_CODE","QID02805A");

			while (!RsECNHDTM.EOF)
			{
				string cPickUpData   = "";
				object cValue;

				_Wxf.AddNew();
				int i=0;
				foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
				{

					cValue = RsECNHDTM[i];

					if (_AttributeMapping.STATUS_CODE != "HIDDEN")
					{
						_Wxf[_AttributeMapping.Name] = cValue;
					}

					if (_AttributeMapping.PrimaryKey)
					{
						if (cPickUpData != "")
						{
							cPickUpData = cPickUpData + "|";
						}
						cPickUpData = cPickUpData + ZZGeneral.Base64Encoder(_AttributeMapping.ColumnName + "=" + cValue);
					}
					i++;
				}

				_Wxf.PK_DATA   = cPickUpData;
				_Wxf.Update();

				RsECNHDTM.MoveNext();
			}
			RsECNHDTM.Close();

			_Entity.Wxf=_Wxf;

			return _Entity;
		}

		public UID02805AE Retrieves(VContexts _VContexts,UID02805AE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02805A", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02805A","NotAccessRight",this.ZZLOGON_ID));
				return _Entity;
			}

			List<UID02805AE> _New_Entitys=new List<UID02805AE>();

				CriteriaRetrieve objRetrieveECNHDTMEntity=new CriteriaRetrieve(typeof(ECNHDTMEntity));

			objRetrieveECNHDTMEntity.Top=1;
			objCondition=objRetrieveECNHDTMEntity.NewCondition();

			bool ZZOrderByAsc=_Setting.getBoolean("ZZOrderByAsc","YES");
			switch (_VContexts.getValue("UDEF_CODE"))
			{
				case "MF":
					{
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",ZZOrderByAsc);
						break;
					}
				case "ML":
					{
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",!ZZOrderByAsc);
						break;
					}
				case "MN":
					{
						if (ZZOrderByAsc)
						{
							objCondition.GreaterThan("ECN_NO",_Entity.ECN_NO);
						}else{
							objCondition.LessThan("ECN_NO",_Entity.ECN_NO);
						}
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",ZZOrderByAsc);
						break;
					}
				case "MP":
					{
						if (ZZOrderByAsc)
						{
							objCondition.LessThan("ECN_NO",_Entity.ECN_NO);
						}else{
							objCondition.GreaterThan("ECN_NO",_Entity.ECN_NO);
						}
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",!ZZOrderByAsc);
						break;
					}
				case "CANCEL":
					{
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",ZZOrderByAsc);
						_Entity.DATA_STATUS_CODE="Verified";
						break;
					}
				case "LOOKUP":
					{
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",ZZOrderByAsc);
						break;
					}

				case "ACTIVETAB":
					{
								if (_Entity.ECN_NO != "")
								{
									objCondition.Equals("ECN_NO", _Entity.ECN_NO);
								}

						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",ZZOrderByAsc);
						break;
					}

				case "READ":
					{
								objCondition.Equals("ECN_NO", _Entity.ECN_NO);
						break;
					}					
				default:
					{
								if (_Entity.ECN_NO != "")
								{
									objCondition.Equals("ECN_NO", _Entity.ECN_NO);
								}
						objRetrieveECNHDTMEntity.OrderBy("ECN_NO",ZZOrderByAsc);

						break;
					}
			}

			//filter by quick look
			string _LOOUP_COLUMN_NAME=";ECNHDTM.ECN_NO;ECNHDTM.ODTP_STATUS;ECNHDTM.ODTP_CODE;ECNHDTM.COMP_CODE;ECNHDTM.ECN_ODTP_RMKS";
			string lookup=_VContexts.getValue("lookup");
			_LOOUP_COLUMN_NAME=_LOOUP_COLUMN_NAME+";";
			if (lookup!="")
			{
				Condition _Condition2=objCondition.NewCondition(" AND ");
				foreach (string s in _LOOUP_COLUMN_NAME.Split(new char[] { ';' }))
				{
					if (!string.IsNullOrEmpty(s))
					{
						Condition OrCondition=_Condition2.NewCondition();
						OrCondition.Expression(s,lookup);
					}
				}
			}

			//filter by filters

				//filter by AccessConditions
				_DataPermission.AccessCondition(objCondition);

			ECNHDTMEntity objECNHDTMs=(ECNHDTMEntity)this.Trans.RetrieveEntity(objRetrieveECNHDTMEntity);

			ZZTrace.Debug(ZFILE_NAME,"SELECT="+objRetrieveECNHDTMEntity.SELECTClause);

			if (objECNHDTMs==null && _VContexts.getValue("UDEF_CODE")=="MN")
			{
				_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataEOF"));
				return _Entity;
			}else if (objECNHDTMs==null && _VContexts.getValue("UDEF_CODE")=="MP")
			{
				_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataBOF"));
				return _Entity;
			}else if (objECNHDTMs==null && (_VContexts.getValue("UDEF_CODE")=="MF" || _VContexts.getValue("UDEF_CODE")=="ML") )
			{
				_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataBlank"));
				return _Entity;
			}else if (objECNHDTMs==null && _VContexts.getValue("UDEF_CODE")=="LOOKUP")
			{
				/*
				 *if (this.COLUMN_REFX.IndexOf(";")>=0)
				 *{
				 *}else{
				 *    string _T_COLUMN_REFX=this.COLUMN_REFX;
				 *    if (_T_COLUMN_REFX.IndexOf('.') > 0)
				 *    {
				 *        string[] s = _T_COLUMN_REFX.Split(new char[] { '.' });
				 *        _T_COLUMN_REFX=s[1];
				 *    }
				 *    _Entity.Exceptions.Add(new LogisticException(_T_COLUMN_REFX,"DataNotFd","DataBlank"));
				 *    return _Entity;
				 *}
				 */
			}else if (objECNHDTMs==null && _VContexts.getValue("UDEF_CODE")=="ACTIVETAB")
			{
				//throw new LogisticException("ECN_NO","Activetab","");
				//this.Default(_Entity);
			}else if (objECNHDTMs==null)
			{
				//this.Default(_Entity);
			}else{
				ECNHDTMEntity objECNHDTMEntity = objECNHDTMs;
					ZZTrace.MUKI_DEBUG_LOG=true;
						_Entity.ECN_NO=objECNHDTMEntity.ECN_NO;
						_Entity.ODTP_XREV=objECNHDTMEntity.ODTP_XREV;
						_Entity.ODTP_STATUS=objECNHDTMEntity.ODTP_STATUS;
						_Entity.ODTP_CODE=objECNHDTMEntity.ODTP_CODE;
						_Entity.COMP_CODE=objECNHDTMEntity.COMP_CODE;
						_Entity.ECN_DATE=objECNHDTMEntity.ECN_DATE;
						_Entity.ECN_ODTP_RMKS=objECNHDTMEntity.ECN_ODTP_RMKS;

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objECNHDTMEntity.ZADD_DATE);
_Entity.UZADD_USER     =objECNHDTMEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objECNHDTMEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objECNHDTMEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objECNHDTMEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objECNHDTMEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objECNHDTMEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objECNHDTMEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objECNHDTMEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objECNHDTMEntity.ZSECURITY_CODE6;

					_Entity.DATA_STATUS_CODE="Verified";
			}
				return _Entity;
		}

		public  UID02805AE AddNew(UID02805AE _Entity)
		{

			ZZTrace.Debug(ZFILE_NAME,"ZZLOGON_ID="+this.ZZLOGON_ID);

			if (_DocsPermission.AccessRight("UID02805A", this.ZZLOGON_ID, "ADDNEW")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02805A","NotAddNewRight",this.ZZLOGON_ID));
				return _Entity;
			}

			_Entity.ATVT_CODE="ADDNEW";

				_Entity=this.Verify(_Entity);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}
			ECNHDTMEntity objAddNewEntity=new ECNHDTMEntity();

			ZZHDKeyLib objZZHDKeyLib=new ZZHDKeyLib();
			objZZHDKeyLib.Trans=this.Trans;
					objZZHDKeyLib.COMP_CODE       = _Entity.COMP_CODE;
					objZZHDKeyLib.ODTP_CODE       = _Entity.ODTP_CODE;
					objZZHDKeyLib.ODTP_CLASS_CODE = "ECN";
					objZZHDKeyLib.Process();
				_Entity.ECN_NO = objZZHDKeyLib.ORD_NO;

					objAddNewEntity.ECN_NO=_Entity.ECN_NO;

			this.Trans.RetrieveEntity(objAddNewEntity);

			if (objAddNewEntity.Verified)
			{
				_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataExist",_Entity.ECN_NO));
				return _Entity;
			}else{

				objAddNewEntity.ZADD_DATE = DateTime.Now;
				objAddNewEntity.ZADD_USER = this.ZZLOGON_ID;

				_DataPermission.TABLE_NAME="ECNHDTM";
				_DataPermission.DataAccessRight();
				objAddNewEntity.ZSECURITY_CODE1 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE1");
				objAddNewEntity.ZSECURITY_CODE2 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE2");
				objAddNewEntity.ZSECURITY_CODE3 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE3");
				objAddNewEntity.ZSECURITY_CODE4 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE4");
				objAddNewEntity.ZSECURITY_CODE5 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE5");
				objAddNewEntity.ZSECURITY_CODE6 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE6");
				objAddNewEntity.ZSECURITY_RIGHT = _DataPermission.ACCESS_AUTH.getInteger("SECURITY_RIGHT");

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZADD_DATE);
_Entity.UZADD_USER     =objAddNewEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objAddNewEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objAddNewEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objAddNewEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objAddNewEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objAddNewEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objAddNewEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objAddNewEntity.ZSECURITY_CODE6;

							objAddNewEntity.ECN_NO=_Entity.ECN_NO;
							objAddNewEntity.ODTP_XREV=_Entity.ODTP_XREV;
							objAddNewEntity.ODTP_STATUS=_Entity.ODTP_STATUS;
							objAddNewEntity.ODTP_CODE=_Entity.ODTP_CODE;
							objAddNewEntity.COMP_CODE=_Entity.COMP_CODE;
							objAddNewEntity.ECN_DATE=_Entity.ECN_DATE;
							objAddNewEntity.ECN_ODTP_RMKS=_Entity.ECN_ODTP_RMKS;
			}

					_Entity.ODTP_STATUS="Draft";
					objAddNewEntity.ODTP_STATUS = _Entity.ODTP_STATUS;

				this.Trans.SaveChanges(objAddNewEntity);

				_Entity.DATA_STATUS_CODE="Verified";
			return _Entity;
		}

		public  UID02805AE Update(UID02805AE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02805A", this.ZZLOGON_ID, "UPDATE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02805A","NotUpdateRight",this.ZZLOGON_ID));
				return _Entity;
			}
			_Entity.ATVT_CODE="UPDATE";

						if (_Entity.ECN_NO=="")
						{
							_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataMiss",""));
						}

			_Entity=this.Verify(_Entity);
			ZZTrace.Debug(ZFILE_NAME,"_Entity.Exceptions.Count="+_Entity.Exceptions.Count);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}

					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ECN_NO="+_Entity.ECN_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ODTP_XREV="+_Entity.ODTP_XREV);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ODTP_STATUS="+_Entity.ODTP_STATUS);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ODTP_CODE="+_Entity.ODTP_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.COMP_CODE="+_Entity.COMP_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ECN_DATE="+_Entity.ECN_DATE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ECN_ODTP_RMKS="+_Entity.ECN_ODTP_RMKS);

			objRetrieveEntity=new CriteriaRetrieve(typeof(ECNHDTMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("ECN_NO", _Entity.ECN_NO);

			_DataPermission.UpdateCondition(objCondition);

			EntityObject objEntityObject=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objEntityObject != null)
			{
				ECNHDTMEntity objUpdateEntity=(ECNHDTMEntity)objEntityObject;
				if (objUpdateEntity.Verified)
				{
					objUpdateEntity.ZCHG_DATE=DateTime.Now;
					objUpdateEntity.ZCHG_USER=this.ZZLOGON_ID;

								objUpdateEntity.ECN_NO=_Entity.ECN_NO;
								objUpdateEntity.ODTP_XREV=_Entity.ODTP_XREV;
								objUpdateEntity.ODTP_STATUS=_Entity.ODTP_STATUS;
								objUpdateEntity.ODTP_CODE=_Entity.ODTP_CODE;
								objUpdateEntity.COMP_CODE=_Entity.COMP_CODE;
								objUpdateEntity.ECN_DATE=_Entity.ECN_DATE;
								objUpdateEntity.ECN_ODTP_RMKS=_Entity.ECN_ODTP_RMKS;

						this.Trans.SaveChanges(objUpdateEntity);

						_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZADD_DATE);
_Entity.UZADD_USER     =objUpdateEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objUpdateEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objUpdateEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objUpdateEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objUpdateEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objUpdateEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objUpdateEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objUpdateEntity.ZSECURITY_CODE6;

					_Entity.DATA_STATUS_CODE="Verified";

				}else{
					_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataNotFd"));
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataNotFd"));
			}
			return _Entity;
		}

		public  UID02805AE Delete(UID02805AE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02805A", this.ZZLOGON_ID, "DELETE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02805A","NotDeleteRight"));
				return _Entity;
			}
			_Entity.ATVT_CODE="DELETE";

					if (_Entity.ECN_NO=="")
					{
						_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataMiss"));
						return _Entity;
					}

				objRetrieveEntity=new CriteriaRetrieve(typeof(ECNHDTMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("ECN_NO", _Entity.ECN_NO);

			_DataPermission.DeleteCondition(objCondition);

			EntityObject objDeleteEntity=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objDeleteEntity != null)
			{
				if (objDeleteEntity.Verified)
				{
						this.Trans.DeleteEntity(objDeleteEntity);

						CriteriaRetrieve objDetailEntity=new CriteriaRetrieve(typeof(ECNDTLMEntity));
						objCondition=objDetailEntity.NewCondition();
						objCondition.Equals("ECN_NO",_Entity.ECN_NO);
			
						List<EntityObject>  objDetailContainer=this.Trans.RetrieveEntitys(objDetailEntity);
			
						for (int i=0;i<objDetailContainer.Count;i++){
								ECNDTLMEntity objECNDTLMEntity=(ECNDTLMEntity)objDetailContainer[i];
								this.Trans.DeleteEntity(objECNDTLMEntity);
						}

						StringBuilder delete_cmd=new StringBuilder(this.ZZLOGON_ID+"UID02805A-DELETED=");
							delete_cmd.Append("ECN_NO="+_Entity.ECN_NO);

					ZZTrace.Debug("DELETED_LOG",delete_cmd.ToString());

				}else{
					_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataNotFd"));
					return _Entity;
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("ECN_NO","DataNotFd"));
			}
			return _Entity;

		}

		public UID02805AE UserDefine(VContexts cMethod,UID02805AE _Entity)
		{
				return _Entity;
		}

#region Field
			private CriteriaRetrieve objRetrieveEntity;
		private List<EntityObject>  objEntityContainer;
		private Condition        objCondition;
		private ZZDefault        objDefault      = new ZZDefault();
		private ZZDataPermission _DataPermission = new ZZDataPermission();
		private ZZDocsPermission _DocsPermission = new ZZDocsPermission();
		private ZZSetting        _Setting        = new ZZSetting();
		private XConvert         _Convert        = new XConvert();

#endregion

			const  string ZFILE_NAME   ="UID02805AM";
		const  string MUKI_EDITION ="MIN";

	}
}
